package com.fzq.web.controller;

import com.fzq.server.dto.RbacUserDto;
import com.fzq.server.mapper.IRbacUserService;
import com.fzq.util.ResponseResult;
import com.fzq.web.vo.RbacUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class RbacUserController {
    @Autowired
    private IRbacUserService iRbacUserService;

    /**
     * 查询所有
     * @return
     */
    @PreAuthorize("hasAuthority('user:list')")
    @GetMapping("/list")
    public ResponseResult<Object> findAll(){
        List<RbacUserDto> userDtoList = iRbacUserService.findAll();
        return new ResponseResult<>(200, userDtoList);
    }

    /**
     * 新增用户
     * @param rbacUserVo
     * @return
     */
    @PreAuthorize("hasAuthority('user:add')")
    @PostMapping("/add")
    public ResponseResult<Void> addBook(@RequestBody RbacUserVo rbacUserVo){
        int i = iRbacUserService.add(rbacUserVo);
        if(i>0){
            return new ResponseResult<Void>(200,"新增成功");
        }else{
            return new ResponseResult<Void>(202,"新增失败");
        }
    }

    /**
     * 通过Id查询
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('user:update')")
    @GetMapping("/findById")
    public ResponseResult<Object> findById(@RequestParam(value = "id",required = false,defaultValue = "") Integer id){
        RbacUserDto rbacUserDto = iRbacUserService.findById(id);
        return new ResponseResult<>(200, rbacUserDto);
    }

    /**
     * 修改用户
     * @param bookVo
     * @return
     */
    @PreAuthorize("hasAuthority('user:update')")
    @PostMapping("/update")
    public ResponseResult<Void> updateBook(@RequestBody RbacUserVo bookVo){
        int i = iRbacUserService.updateUser(bookVo);
        if(i>0){
            return new ResponseResult<Void>(200,"修改成功");
        }else{
            return new ResponseResult<Void>(202,"修改失败");
        }
    }


    /**
     * 删除用户
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('user:delete')")
    @GetMapping("/delete")
    public ResponseResult<Void> deleteById(@RequestParam(value = "id",required = false,defaultValue = "") Integer id){
        int i = iRbacUserService.deleteUser(id);
        if(i>0){
            return new ResponseResult<Void>(200,"删除成功");
        }else{
            return new ResponseResult<Void>(202,"删除失败");
        }
    }

}
